package com.linkwedo.saas.project;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkwedo.saas.datasource.ConnectionInfo;
import com.linkwedo.saas.datasource.ConnectionStrategy;
import com.linkwedo.saas.domain.Project;
import com.linkwedo.saas.domain.member.RegisterCompanyVerify;
import com.linkwedo.saas.domain.member.SaasLog;
import com.linkwedo.saas.domain.member.User;

public class ProjectDao extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
	.getLogger(ProjectDao.class);
	// property constants
	public static final String NAME = "name";
	public static final String LEADER_NAME = "leaderName";
	public static final String GOAL = "goal";
	public static final String DESCRIPTION = "description";
	public static final String BUDGET = "budget";
	public static final String TAG = "tag";
	public static final String COLOR = "color";
	public static final String PIC_PATH = "picPath";
	public static final String NOTE = "note";

	public void save(Project transientInstance) {
		log.debug("saving Project instance");
		Session curSession = getSession();
		try {
			curSession.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void delete(Project persistentInstance) {
		log.debug("deleting com.huayanginfo.saas.domain.Project instance");
		Session curSession = getSession();
		try {
		
			Transaction tr = curSession.beginTransaction();
			curSession.delete(persistentInstance);
			log.debug("delete successful");
			tr.commit();
			
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Project findById(java.lang.Integer id) {
		log.debug("getting Project instance with id: " + id);
		Session curSession = getSession();
		try {
			Project instance = (Project) curSession.get(
					"com.huayanginfo.saas.domain.Project", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByExample(Project instance) {
		log.debug("finding Project instance by example");
		Session curSession = getSession();
		try {
			List results = curSession.createCriteria("com.huayanginfo.saas.domain.Project").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Project instance with property: " + propertyName
				+ ", value: " + value);
		Session curSession = getSession();
		try {
			String queryString = "from Project as model where model."
					+ propertyName + "= ?";
			Query queryObject = curSession.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByLeaderName(Object leaderName) {
		return findByProperty(LEADER_NAME, leaderName);
	}

	public List findByGoal(Object goal) {
		return findByProperty(GOAL, goal);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByBudget(Object budget) {
		return findByProperty(BUDGET, budget);
	}

	public List findByTag(Object tag) {
		return findByProperty(TAG, tag);
	}

	public List findByColor(Object color) {
		return findByProperty(COLOR, color);
	}

	public List findByPicPath(Object picPath) {
		return findByProperty(PIC_PATH, picPath);
	}

	public List findByNote(Object note) {
		return findByProperty(NOTE, note);
	}

	public List findAll() {
		log.debug("finding all Project instances");
		Session curSession = getSession();
		try {
			String queryString = "from Project where status=1";
			Query queryObject = curSession.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Project merge(Project detachedInstance) {
		log.debug("merging Project instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			Project result = (Project) curSession.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachDirty(Project instance) {
		log.debug("attaching dirty Project instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.saveOrUpdate(instance);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} finally{
			curSession.close();
		}
	}

	public void attachClean(Project instance) {
		log.debug("attaching clean Project instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public static void main(String args[]) {
		ProjectDao p = new ProjectDao();
		List list;
		try {
			list = p.findAll();
			Project pro=new Project();
		 	for(int i=0;i<list.size();i++){
		 	 	pro=(Project)list.get(i); 
		 	 	System.out.println(pro.getName());
		 	}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
